package com.atguigu.aspect;

import com.atguigu.annotations.CustomLogAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @auther zzyy
 * @create 2024-12-28 12:01
 */
@Aspect
@Component
@Slf4j
public class CustomLogAspect
{
    @Around("@annotation(customLogAnnotation)")
    public Object logAround(ProceedingJoinPoint proceedingJoinPoint, CustomLogAnnotation customLogAnnotation) throws Throwable
    {
        Object result = null;

        if (customLogAnnotation.enable())
        {
            long startTime = System.currentTimeMillis();

            // 1 方法执行前的处理
            System.out.println("Before method execution: " + proceedingJoinPoint.getSignature().getName());
            // 2 执行目标方法主业务逻辑,放行
            result = proceedingJoinPoint.proceed();
            // 3 方法执行后的处理
            System.out.println("After method execution: " +  customLogAnnotation.value());

            long endTime = System.currentTimeMillis();
            System.out.println("----costTime: "+(endTime - startTime) +" 毫秒");

            return result;
        } else {
            return proceedingJoinPoint.proceed();
        }
    }
}
